package
{
	import com.xcopy.Screen;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class Slot25Debugger extends Slot25Standard
	{
		private const CONTENT_MAX:Number    = 40960;
		private const CONTENT_LESS:Number   = CONTENT_MAX * 0.4;
		private const CONTENT_SCROLL:Number = 100000;
		public var commandExample:Array     = []; // 所有 function 的 cmd 及 參數列舉
		public var autoType:Number          = 0; // 0:沒事  1:登入工程模式  2:登入遊戲主機
		public var token:String             = '';

		public function Slot25Debugger(r:* = null)
		{
			super(r);
			commandExample['EngineerLogin'] = 'TS000088';
		}

		public override function set clip(c:MovieClip):void
		{
			_clip = c;
			setClickEvent(clip.btnConnect, clickLink);
			setClickEvent(clip.btnSend, clickSend);
			setClickEvent(clip.btnSample, clickSample);
			setClickEvent(clip.btnTest1, clickTest1);
			setClickEvent(clip.btnTest2, clickTest2);
			setClickEvent(clip.btnTest3, clickTest3);
			setClickEvent(clip.btnTest4, clickTest4);
			setClickEvent(clip.oLight, clickClose);
			clip.cmbType.addEventListener(Event.CHANGE, clickSample);
		}

		private function clickTest1(e:MouseEvent):void
		{
			this.core.addTalking('here comes a new challenger!!');
			this.core.addMessage('here comes a new challenger!!');
		}

		private function clickTest2(e:MouseEvent):void
		{
			this.main.endGame();
			this.client.disconnect();
		}

		private function clickTest3(e:MouseEvent):void
		{
			//this.client.C2S_Talk('free');
			print('嘗試直接登入');
			this.main.createGame('203.67.80.200', 20013, clip.txtError.text, '');
		}

		private function clickTest4(e:MouseEvent):void
		{
			print('嘗試進入工程模式');
			this.main.createEngineer('10.10.50.39', 20018, 'TS000129', ''); // 景堯
		}

		private function clickClose(e:MouseEvent):void
		{
			disable();
		}

		public function manualLink(IP:String, port:Number):void
		{
			this.client.connect(IP, port);
		}

		public function clickLink(e:MouseEvent):void
		{
			this.client.connect(clip.cmbIP.text, Number(clip.cmbPort.text));
		}

		public function clickSend(e:MouseEvent):void
		{
			if (clip.txtMessage.text == '')
			{
				return;
			}
			sendCommand(clip.cmbType.value, clip.txtMessage.text);
			clip.txtMessage.text = '';
		}

		public function clickSample(e:Event = null):void
		{
			if (commandExample[clip.cmbType.value])
			{
				clip.txtMessage.text = commandExample[clip.cmbType.value];
			}
			else
			{
				clip.txtMessage.text = '無此指令';
			}
		}

		public function green():void
		{
			clip.oLight.gotoAndStop('Green');
		}

		public function yellow():void
		{
			clip.oLight.gotoAndStop('Yellow');
		}

		public function red():void
		{
			clip.oLight.gotoAndStop('Red');
		}

		public function alert(str:String):void
		{
			clip.txtError.appendText(str + "\n");
			clip.txtError.verticalScrollPosition = CONTENT_SCROLL;
		}

		public function print(str:String):void
		{
			clip.txtContent.appendText(str + "\n");
			if (clip.txtContent.text.length > CONTENT_MAX)
			{
				clip.txtContent.text = clip.txtContent.text.substr(CONTENT_LESS);
			}
			clip.txtContent.verticalScrollPosition = CONTENT_SCROLL;
		}

		public function log(str:String):void
		{
			clip.txtLog.appendText(str + "\n");
			if (clip.txtLog.text.length > CONTENT_MAX)
			{
				clip.txtLog.text = clip.txtLog.text.substr(CONTENT_LESS);
			}
			clip.txtLog.verticalScrollPosition = CONTENT_SCROLL;
		}

		public function dump(str:String):void
		{
			clip.txtDump.text = str;
			clip.txtDump.verticalScrollPosition = CONTENT_SCROLL;
		}

		public function sendBytes(str:String):void
		{
			var i:Number     = 0;
			var ba:ByteArray = new ByteArray();
			ba.endian = Endian.LITTLE_ENDIAN;
			var sa:Array = str.split(',');
			for (i = 0; i < sa.length; i++)
			{
				ba.writeByte(sa[i]);
			}
			this.client.pushByte(ba);
		}

		public function sendCommand(cmd:String, str:String):void
		{
			var args:Array = [];
			switch (cmd)
			{
				case 'Byte': // 不做任何處理直接將 byte array 送出，測試最原始封包使用
				{
					sendBytes(str);
					break;
				}
				case '1015':
				case 'EngineerLogin':
				{
					args = str.split("\r");
					this.client.C2S_EngineerLogin(args[0]);
					break;
				}
			}
		}
	}
}
